package cn.itcast.tanhua.sso.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * @ClassName MyMetaObjectHandler
 * @Description MP自动填充处理器
 * @Author WU
 * @Date 2021/5/23 20:54
 * @Version 1.0
 */
public class MyMetaObjectHandler implements MetaObjectHandler {

//    添加新的字段时，创建和修改的日期都要填充
    @Override
    public void insertFill(MetaObject metaObject) {
//        通过反射原理获取字段
        Object created = getFieldValByName("created", metaObject);
//        如果字段为空则填充
        if (created == null) {
            setFieldValByName("created",new Date(),metaObject);
        }

        Object updated = getFieldValByName("updated", metaObject);
        if (updated == null) {
            setFieldValByName("updated",new Date(),metaObject);
        }
    }

//    修改字段时，直接更新修改日期即可
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updated",new Date(),metaObject);
    }
}
